package com.cmict.data.bpm.api.task;

import com.cmict.data.bpm.api.instance.BpmProcessInstanceApi;
import com.cmict.data.bpm.api.instance.dto.BpmProcessInstanceCreateReqDTO;
import com.cmict.data.bpm.api.instance.dto.BpmProcessInstancePageItemRespDTO;
import com.cmict.data.bpm.api.instance.dto.BpmProcessInstancePageReqDTO;
import com.cmict.data.bpm.api.instance.dto.BpmProcessInstanceStatusDTO;
import com.cmict.data.bpm.controller.admin.task.vo.instance.BpmProcessInstanceStatusReqVo;
import com.cmict.data.bpm.convert.task.BpmProcessInstanceConvert;
import com.cmict.data.bpm.service.task.BpmProcessInstanceService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * Flowable 流程实例 Api 实现类
 *
 * @author awesome
 * @author jason
 */
@RestController
@Validated
public class BpmProcessInstanceApiImpl implements BpmProcessInstanceApi {

    @Resource
    private BpmProcessInstanceService processInstanceService;

    @Override
    public String createProcessInstance(Long userId, BpmProcessInstanceCreateReqDTO reqDTO) {
        return processInstanceService.createProcessInstance(userId, reqDTO);
    }

    @Override
    public CommonResult<PageResult<BpmProcessInstancePageItemRespDTO>> getInstance(Long userId, BpmProcessInstancePageReqDTO reqDTO) {
        PageResult<BpmProcessInstancePageItemRespDTO> processInstancePage = processInstanceService.getProcessInstancePage(userId, reqDTO);
        return CommonResult.success(processInstancePage);
    }

    @Override
    public CommonResult<BpmProcessInstanceStatusDTO> getInstanceStatus(String formId, String recordId) {
        BpmProcessInstanceStatusReqVo processInstanceStatus = processInstanceService.getProcessInstanceStatus(Long.valueOf(formId), Long.valueOf(recordId));
        BpmProcessInstanceStatusDTO bpmProcessInstanceStatusDTO = BpmProcessInstanceConvert.INSTANCE.convertStatus(processInstanceStatus);
        return CommonResult.success(bpmProcessInstanceStatusDTO);
    }

    @Override
    public Boolean bindProcess(Long formId) {
        return processInstanceService.isBoundFormAndProcess(formId);
    }

    @Override
    public CommonResult<Boolean> updateFormVariable(String bpmInstanceId,Long newMenuId,Long newDeptId) {
        return CommonResult.success(processInstanceService.updateFormVariables(bpmInstanceId,newMenuId,newDeptId));
    }

}
